package softtech.apps.callblocker.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHanler extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "database_call_blocker";

	public DatabaseHanler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_SETTING);
		db.execSQL(CREATE_TABLE_BLACK_LIST_ITEM);
		db.execSQL(CREATE_TABLE_CALL_BLOCK_ITEM);
		db.execSQL(CREATE_TABLE_SMS_BLOCK_ITEM);
		db.execSQL(CREATE_TABLE_CONTENT_SMS_BLOCK);
		db.execSQL(CREATE_TABLE_HEADER_PHONE_CALL);
		db.execSQL(CREATE_TABLE_HEADER_PHONE_SMS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_SETTING);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_BLACK_LIST_ITEM);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_CALL_BLOCK_ITEM);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_SMS_BLOCK_ITEM);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_CONTENT_SMS_BLOCK);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_HEADER_PHONE_CALL);
		db.execSQL("DROP TABLE IF EXISTS " + DbConstant.TABLE_HEADER_PHONE_SMS);
		onCreate(db);
	}

	// Setting create statement
	private static final String CREATE_TABLE_SETTING = "CREATE TABLE "
			+ Setting.TABLE_NAME + "(" + Setting.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + DbConstant.PASS_CODE
			+ " TEXT, " + DbConstant.PASS_FAKE_CODE + " TEXT, "
			+ Setting.DEFAULT_MODE + " INTEGER, "
			+ Setting.BLOCK_CALL_SETTING + " INTEGER, "
			+ Setting.BLOCK_SMS_SETTING + " INTEGER " + ")";

	// Blacklist Item create statement
	private static final String CREATE_TABLE_BLACK_LIST_ITEM = "CREATE TABLE "
			+ BlackListItem.TABLE_NAME + "(" + BlackListItem.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + BlackListItem.PHONE_NUMBER
			+ " TEXT, " + BlackListItem.NAME + " TEXT, " + BlackListItem.MODE
			+ " INTEGER )";

	// Call block Item create statement
	private static final String CREATE_TABLE_CALL_BLOCK_ITEM = "CREATE TABLE "
			+ CallBlockedItem.TABLE_NAME + "(" + CallBlockedItem.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + CallBlockedItem.PHONE_NUMBER
			+ " TEXT, " + CallBlockedItem.DATE_TIME + " DATETIME, "
			+ CallBlockedItem.STATUS + " INTEGER )";

	// sms block item create statement
	private static final String CREATE_TABLE_SMS_BLOCK_ITEM = "CREATE TABLE "
			+ SmsBlockedItem.TABLE_NAME + "(" + SmsBlockedItem.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + SmsBlockedItem.PHONE_NUMBER
			+ " TEXT, " + SmsBlockedItem.DATE_TIME + " DATETIME, "
			+ SmsBlockedItem.STATUS + " INTEGER, " + SmsBlockedItem.CONTENT_SMS
			+ " TEXT )";
	

	private static final String CREATE_TABLE_CONTENT_SMS_BLOCK = "CREATE TABLE "
			+ ContentSmsBlock.TABLE_NAME
			+ "("
			+ ContentSmsBlock.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ ContentSmsBlock.CONTENT
			+ " TEXT )";

	private static final String CREATE_TABLE_HEADER_PHONE_CALL = "CREATE TABLE "
			+ HeaderPhone.TABLE_NAME
			+ "("
			+ HeaderPhone.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ HeaderPhone.HEADER
			+ " TEXT )";

	private static final String CREATE_TABLE_HEADER_PHONE_SMS = "CREATE TABLE "
			+ "headerPhoneSms (" + HeaderPhone.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + HeaderPhone.HEADER
			+ " TEXT )";
	private static final String TAG = null;

	
	
	
	/**
	 * insert row into table Setting
	 * 
	 * @param setting
	 * @return the row ID of the newly inserted row
	 */
	public long insertSetting(Setting setting) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = setting.getValuesSetting();
		long rowId = db.insert(Setting.TABLE_NAME, null, values);
		return rowId;
	}

	/**
	 * udate row in table setting
	 * 
	 * @param setting
	 * @return number of row affect
	 */
	public int updateSetting(Setting setting) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = setting.getValuesSetting();
		return db.update(Setting.TABLE_NAME, values, Setting.ID + " = ?",
				new String[] { String.valueOf(setting.getId()) });
	}

	/**
	 * update PassCode in Setting
	 * 
	 * @param passCode
	 */
	public void updateSettingPassCode(String passCode) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + Setting.TABLE_NAME + " SET "
				+ Setting.PASS_CODE + " =  '" + passCode + "' ";
		db.execSQL(sql);
	}

	/**
	 * upadte passFakeCode Setting
	 * 
	 * @param passFakeCode
	 */
	public void updateSettingPassFakeCode(String passFakeCode) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + Setting.TABLE_NAME + " SET "
				+ Setting.PASS_FAKE_CODE + " =  '" + passFakeCode + "' ";
		db.execSQL(sql);
	}

	/**
	 * update Default_model in Setting
	 * 
	 * @param setDefault
	 */
	public void updateSettingDefault(int setDefault) {
		SQLiteDatabase db = this.getWritableDatabase();
		
//		Setting s = this.getSetting(null);
//		ContentValues values = new ContentValues();
//		values.put(DbConstant.PASS_CODE, s.getPassCode());
//		values.put(DbConstant.PASS_FAKE_CODE, s.getPassFakeCode());
//		values.put(DbConstant.DEFAULT_MODE, setDefault);
//		values.put(DbConstant.BLOCK_CALL_SETTING, s.getBlockCallSetting());
//		values.put(DbConstant.BLOCK_SMS_SETTING, s.getBlockSmsSetting());
//		return db.update(Setting.TABLE_NAME, values, null, null);
		String sql = "UPDATE " + Setting.TABLE_NAME + " SET "
				+ Setting.DEFAULT_MODE + " = " + setDefault;
		db.execSQL(sql);
	}

	/**
	 * update BlockCallSeting in SEtting
	 * 
	 * @param blockCall
	 */
	public void updateSettingBlockCall(int blockCall) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + Setting.TABLE_NAME + " SET "
				+ Setting.BLOCK_CALL_SETTING + " = " + blockCall;
		db.execSQL(sql);
	}

	/**
	 * e update BlockSmsSetting in Setting
	 * 
	 * @param blockSms
	 */
	public void updateSettingBlockSms(int blockSms) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + Setting.TABLE_NAME + " SET "
				+ Setting.BLOCK_SMS_SETTING + " = " + blockSms;
		db.execSQL(sql);
	}

	/**
	 * detete row in table setting
	 * 
	 * @param id
	 * @return
	 */
	public int deleteSetting(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete(Setting.TABLE_NAME, Setting.ID + " = " + id, null);
	}

	/**
	 * 
	 * @param columns
	 * @return
	 */
	public Setting getSetting(String[] columns) {
		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + Setting.TABLE_NAME;
		Cursor cursor;
		cursor = db.query(Setting.TABLE_NAME, columns, null, null, null, null,
				null, null);
		if (cursor.moveToFirst()) {
			Setting setting = new Setting();
			setting = Setting.SetValueFromCursor(cursor);
			Log.v(TAG,"id---------- :" + setting.getId());
			return setting;
		} else
			return null;
	}

	/**
	 * inserting a row into the table blacklistitem database.
	 * 
	 * @param BlackListItem
	 *            item
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertBlackListItem(BlackListItem item) {
		
		if (item== null || getBlackListItem(null, BlackListItem.PHONE_NUMBER+"="+item.getPhoneNumber(), null, null, null).size() >0  ){
			Log.i(TAG,"size: vo phuoc "+getBlackListItem(null, BlackListItem.PHONE_NUMBER+"="+item.getPhoneNumber(), null, null, null).size());
			return -1;
		}
		if(getBlackListItem(null, BlackListItem.PHONE_NUMBER + " = " + item.getPhoneNumber(), null, null, null).equals(null)){
			Log.i(TAG,"vo mui");
			return -1;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		return db.insert(BlackListItem.TABLE_NAME, null, values);
	}

	/**
	 * update row in BlackListItem
	 * 
	 * @param item
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateBlackListItem(BlackListItem item, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		int number = db.update(DbConstant.TABLE_BLACK_LIST_ITEM, values,
				whereClause, whereArgs);
		Log.i(TAG,"updated");
		return number;
	}

	public void updateModeInBlackList(int mode, int id){
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + BlackListItem.TABLE_NAME 
				+ " SET " + BlackListItem.MODE + " = " + mode 
				+ " WHERE " +BlackListItem.ID + " = " + id;
		db.execSQL(sql);
	}
	public void updateNameInBlackList(String name, int id){
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + BlackListItem.TABLE_NAME 
				+ " SET " + BlackListItem.NAME + " = '"+name+"'" 
				+ " WHERE " +BlackListItem.ID + " = " + id;
		db.execSQL(sql);
	}
	/**
	 * deleting rows in the table BlackListItem.
	 * 
	 * @param id
	 * @return the number of rows affected
	 */
	public int deleteBlackListItem(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = 0;
		try {
			number = db
					.delete(BlackListItem.TABLE_NAME, whereClause, whereArgs);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		return number;
	}

	/**
	 * get a row blacklistitem
	 * 
	 * @param id
	 * @return blacklistitem
	 */
	public List<BlackListItem> getBlackListItem(String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<BlackListItem> blackList = new ArrayList<BlackListItem>();

		Cursor cursor = db.query(BlackListItem.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);

		if (cursor.moveToFirst()) {
			do {
				BlackListItem item = new BlackListItem();
				item = BlackListItem.setValueFromCursor(cursor);
				blackList.add(item);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();

		return blackList;
	}

	/**
	 * insert inserting a row into the database.
	 * 
	 * @param item
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertCallBlockItem(CallBlockedItem item) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		return db.insert(CallBlockedItem.TABLE_NAME, null, values);

	}

	/**
	 * updating rows in the database.
	 * 
	 * @param CallBlockedItem
	 *            item
	 * @return the number of rows affected
	 */
	public int updateCallBlockItem(CallBlockedItem item, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		int number = db.update(CallBlockedItem.TABLE_NAME, values, whereClause,
				whereArgs);
		return number;
	}

	
	/**
	 * delete rows in CallBlockItem
	 * g
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int deleteCallBlockItem(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = db.delete(CallBlockedItem.TABLE_NAME, whereClause,
				whereArgs);
		return number;

	}

	/**
	 * Method to query select in CallBlockITem
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param limit
	 * @return List<CallBlockItem>
	 */
	
	public List<CallBlockedItem> getCallBlockItem(String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<CallBlockedItem> list = new ArrayList<CallBlockedItem>();

		Cursor cursor = db.query(CallBlockedItem.TABLE_NAME, columns,
				selection, selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				CallBlockedItem item = new CallBlockedItem();
				item = CallBlockedItem.setValueFromCursor(cursor);
				list.add(item);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();

		return list;

	}

	/**
	 * insert inserting a row into SmsBlockItem
	 * 
	 * @param SmsBlockedItem
	 *            item
	 * @returnthe row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertSmsBlockItem(SmsBlockedItem item) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		return db.insert(SmsBlockedItem.TABLE_NAME, null, values);
	}

	/**
	 * update row in SmsBlockItem
	 * 
	 * @param item
	 * @param whereClause
	 * @param whereArgs
	 * @return the number of rows affected
	 */
	public int updateSmsBlockItem(SmsBlockedItem item, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = item.getValuesSetting();
		return db.update(SmsBlockedItem.TABLE_NAME, values, whereClause,
				whereArgs);
	}

	/**
	 * delete row in SmsBlockItem
	 * 
	 * @param whereClause
	 * @param whereArgs
	 * @returnthe number of rows affected
	 */
	public int deleteSmsBlockItem(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();

		int number = db.delete(SmsBlockedItem.TABLE_NAME, whereClause,
				whereArgs);
		return number;
	}

	/**
	 * get List<SmsBlockITem>
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return List<SmsBlockITem>
	 */
	public ArrayList<SmsBlockedItem> getSmsBlockItem(String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		ArrayList<SmsBlockedItem> list = new ArrayList<SmsBlockedItem>();
		Cursor cursor = db.query(SmsBlockedItem.TABLE_NAME, columns, selection,
				selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				SmsBlockedItem item = new SmsBlockedItem();
				item = SmsBlockedItem.setValueFromCursor(cursor);
				list.add(item);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();

		return list;
	}

	/**
	 * insert row ContentSmsBlock
	 * 
	 * @param contentSms
	 * @return
	 */
	public long insertContentSmsBlock(ContentSmsBlock contentSms) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = contentSms.getValues();
		// values.put(contentSms.CONTENT, contentSms.getContent());
		long number = db.insert(ContentSmsBlock.TABLE_NAME, null, values);
		return number;
	}

	/**
	 * insert row into HeaderPhoneCall
	 * 
	 * @param phone
	 * @return
	 */
	public long insertHeaderPhoneCall(HeaderPhone phone) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(HeaderPhone.HEADER, phone.getHeader());
		long number = db.insert(DbConstant.TABLE_HEADER_PHONE_CALL, null,
				values);
		return number;
	}

	/**
	 * insert row into HeaderphoneSms
	 * 
	 * @param phone
	 * @return
	 */
	public long insertHeaderPhoneSms(HeaderPhone phone) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(HeaderPhone.HEADER, phone.getHeader());
		long number = db
				.insert(DbConstant.TABLE_HEADER_PHONE_SMS, null, values);
		return number;
	}

	/**
	 * delete row from table ContentSmsBlock
	 * 
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int deleteContentSmsBlock(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = db.delete(ContentSmsBlock.TABLE_NAME, whereClause,
				whereArgs);
		return number;
	}

	/**
	 * delete row from table headerPhoneCall
	 * 
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int deleteHeaderPhoneCall(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = db.delete(DbConstant.TABLE_HEADER_PHONE_CALL, whereClause,
				whereArgs);
		return number;
	}

	/**
	 * delete row from table HeaderPhoneSms
	 * 
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int deleteHeaderPhoneSms(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		int number = db.delete(DbConstant.TABLE_HEADER_PHONE_SMS, whereClause,
				whereArgs);
		return number;
	}

	/**
	 * update row in ContentSmsBlock
	 * 
	 * @param contentSms
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int updateContentSmsBlock(ContentSmsBlock contentSms,
			String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(contentSms.CONTENT, contentSms.getContent());
		int number = db.update(ContentSmsBlock.TABLE_NAME, values, whereClause,
				whereArgs);
		return number;
	}

	/**
	 * update row in HeaderPhoneCall
	 * 
	 * @param contentSms
	 * @param phone
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int updateHeaderPhoneCall(HeaderPhone phone, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(HeaderPhone.HEADER, phone.getHeader());
		int number = db.update(HeaderPhone.TABLE_NAME, values,
				whereClause, whereArgs);
		return number;
	}

	/**
	 * update row in HeaderPhoneSms
	 * 
	 * @param contentSms
	 * @param phone
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public int updateHeaderPhoneSms(HeaderPhone phone, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(HeaderPhone.HEADER, phone.getHeader());
		int number = db.update(DbConstant.TABLE_HEADER_PHONE_SMS, values,
				whereClause, whereArgs);
		return number;
	}

	/**
	 * get ContentSmsBlock
	 * 
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public List<ContentSmsBlock> getContentSmsBlocks(String[] columns,
			String selection, String[] selectionArgs, String orderBy,
			String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<ContentSmsBlock> list = new ArrayList<ContentSmsBlock>();
		Cursor cursor = db.query(ContentSmsBlock.TABLE_NAME, columns,
				selection, selectionArgs, null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				ContentSmsBlock content = new ContentSmsBlock();
				content = ContentSmsBlock.setValueFromCursor(cursor);
				list.add(content);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		return list;

	}

	/**
	 * get HeaderPhoneCall and HEaderPhoneSms
	 *  
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public ArrayList<HeaderPhone> getHeaderPhones(
			String[] columns, String selection, String[] selectionArgs,
			String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		ArrayList<HeaderPhone> list = new ArrayList<HeaderPhone>();
		Cursor cursor = db.query(HeaderPhone.TABLE_NAME, columns, selection, selectionArgs,
				null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				HeaderPhone headerPhone = new HeaderPhone();
				headerPhone = HeaderPhone.setValueFromCursor(cursor);
				list.add(headerPhone);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		return list;

	}
	
	/**
	 * get HeaderPhoneCall and HEaderPhoneSms
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public List<HeaderPhone> getHeaderPhones(String tableName,
			String[] columns, String selection, String[] selectionArgs,
			String orderBy, String limit) {
		SQLiteDatabase db = this.getReadableDatabase();
		List<HeaderPhone> list = new ArrayList<HeaderPhone>();
		Cursor cursor = db.query(tableName, columns, selection, selectionArgs,
				null, null, orderBy, limit);
		if (cursor.moveToFirst()) {
			do {
				HeaderPhone headerPhone = new HeaderPhone();
				headerPhone = HeaderPhone.setValueFromCursor(cursor);
				list.add(headerPhone);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
			cursor.close();
		return list;

	}
	
}
